package cn.com.dhcc.turbo.service.exch;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.Md5Util;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;
import cn.com.dhcc.app.pub.core.consts.KernelParam;
import cn.com.dhcc.turbo.service.test.NodeExchTestService;


/**
 *  通过api接口调用的客户端验证接口实现
 * @author qiao
 * @2015-1-7 @下午6:01:13
 */
@Service(AppIocBean.APIUSER_AUTHSERVICE_IMPL)
public class ApiUserAuthServiceImpl implements ApiUserAuthService {
	
	private static Logger logger = Log.getLogger(LOGTYPE.FTP);
	@Override
	public boolean isAdapterAuthCodeOkForFtp( FtpApiUserInfo ftpApiUserInfo) {
		if (!KernelParam.authValidate) {
			return true;
		}
		String ip=ftpApiUserInfo.getUserMetadata().getInetAddress().getHostAddress();
		String adapterCode = ftpApiUserInfo.getAdapterCode();
		String authCode = ftpApiUserInfo.getAuthCode();		
		try {
			if (adapterCode.equals(NodeExchTestService.TEST_ADAPTER_CODE) && authCode.equals(NodeExchTestService.TEST_ADAPTER_AUTHORIZATION)) {
				return true;
			}
			return createAuthCode(adapterCode, ip).equals(authCode);
		} catch (ServiceException e) {
			logger.error(e);
			return false;
		}
	}
	
	@Override
	public boolean isAdapterAuthCodeOkForWebservice(
			WebServiceClientUserInfo webServiceClientUserInfo) {
		String ip=webServiceClientUserInfo.getUserMetadata().getInetAddress().getHostAddress();
		
		String adapterCode = webServiceClientUserInfo.getAdapterCode();
		String authCode = webServiceClientUserInfo.getAuthCode();		
		try {
			boolean equal =  createAuthCode(adapterCode, ip).equals(authCode);
			if(!equal&&"127.0.0.1".equalsIgnoreCase(ip)){
				//@TODO 如果是本地ip例如：127.0.0.1 则说明适配器ip和平台ip一样。此时只需要验证和平台ip一样的适配器是否有授权码即可
				String localIp = AppConst.installIp;
				equal =  createAuthCode(adapterCode, localIp).equals(authCode);
			}
			return equal;
		} catch (ServiceException e) {
			logger.error(e);
			return false;
		}
	}
	@Override
	public boolean isAdapterAuthCodeOkForHttp(HttpClientUserInfo httpClientUserInfo) {
		String ip=httpClientUserInfo.getUserMetadata().getInetAddress().getHostAddress();
		
		String adapterCode = httpClientUserInfo.getAdapterCode();
		String authCode = httpClientUserInfo.getAuthCode();		
		try {
			boolean equal =  createAuthCode(adapterCode, ip).equals(authCode);
			if(!equal&&"127.0.0.1".equalsIgnoreCase(ip)){
				//@TODO 如果是本地ip例如：127.0.0.1 则说明适配器ip和平台ip一样。此时只需要验证和平台ip一样的适配器是否有授权码即可
				String localIp = AppConst.installIp;
				equal =  createAuthCode(adapterCode, localIp).equals(authCode);
			}
			return equal;
		} catch (ServiceException e) {
			logger.error(e);
			return false;
		}
	}
	@Override
	public String createAuthCode(String adapter, String hostUrl) throws ServiceException {
		InetAddress address;
		try {
			address = InetAddress.getByName(hostUrl);
		} catch (UnknownHostException e) {
			throw new ServiceException(e);
		}
		return createAuthCodeStatic(adapter+"_"+address.getHostAddress());
	}
	public static String createAuthCodeStatic(String source){
		return Md5Util.encryptMD5(source);
	}
}
